package com.stationpc.dock

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.stationpc.dock.service.DockOverlayService
import androidx.core.content.ContextCompat

class LongPressMenuReceiver : BroadcastReceiver() {

    companion object {
        private const val TAG = "LongPressMenuReceiver"
        private const val SYSTEM_DIALOG_REASON_KEY = "reason"
        private const val SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"
    }

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
            val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY)
            if (reason == SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS) {
                Log.d(TAG, "Long press menu button detected")
                toggleDockVisibility(context)
            }
        }
    }

    private fun toggleDockVisibility(context: Context) {
        val intent = Intent(context, DockOverlayService::class.java)
        intent.action = DockOverlayService.ACTION_TOGGLE_VISIBILITY
        ContextCompat.startForegroundService(context, intent)
    }
}
